<?php
/**
 * Recycle Bin Controller
 *
 * @author hiscaler <hiscaler@gmail.com>
 * @link http://www.yuandot.com/
 * @copyright Copyright &copy; 2009-2010 Yuandot Information Technology CO.,LTD.
 * @license http://doit.yuandot.com/license/
 */

class RecycleBinController extends Controller {
    public $layout='contents';
    const PAGE_SIZE = 12;

    public function filters() {
        return array(
            'accessControl',
        );
    }

    public function accessRules() {
        return array(
//            array('allow', 'actions' => array(
//                    'index', 'product', 'post', 'userCase', 'personnel', 'video',
//                    'music', 'link', 'slide', 'feedback', 'book', 'download'
//                ),
//                'users' => array('*'),
//            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array(
                    'index', 'product', 'post', 'userCase', 'personnel', 'video',
                    'music', 'link', 'slide', 'feedback', 'book', 'download', 'album',
                    'todayOnHistory', 'Job'
                ),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'batchProcess' actions
                'actions' => array('admin', 'batchProcess'),
                'users' => array('admin'),
            ),
            array('deny',  // deny all users
                'users' => array('*'),
            ),
        );
    }

    // 主界面
    public function actionIndex() {
        $this->renderPartial('index', null, false, true);
    }

    // 信息
    public function actionPost() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Post::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Post');
        $sort->applyOrder($criteria);

        $models = Post::model()->findAll($criteria);

        $this->render('post', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 产品
    public function actionProduct() {
    }

    // 案例
    public function actionUserCase() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(UserCase::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('UserCase');
        $sort->applyOrder($criteria);

        $models = UserCase::model()->findAll($criteria);

        $this->render('userCase', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 人物介绍
    public function actionPersonnel() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Personnel::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Personnel');
        $sort->applyOrder($criteria);

        $models = Personnel::model()->findAll($criteria);

        $this->render('personnel', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 视频
    public function actionVideo() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Video::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Video');
        $sort->applyOrder($criteria);

        $models = Video::model()->findAll($criteria);

        $this->render('video', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 音乐
    public function actionMusic() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Music::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Music');
        $sort->applyOrder($criteria);

        $models = Music::model()->findAll($criteria);

        $this->render('music', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }
    
    // 友情链接
    public function actionLink() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Link::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Link');
        $sort->applyOrder($criteria);

        $models = Link::model()->findAll($criteria);

        $this->render('link', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }
    
    // 幻灯片
    public function actionSlide() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Slide::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Slide');
        $sort->applyOrder($criteria);

        $models = Slide::model()->findAll($criteria);

        $this->render('slide', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 留言
    public function actionFeedback() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Feedback::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Feedback');
        $sort->applyOrder($criteria);

        $models = Feedback::model()->findAll($criteria);

        $this->render('feedback', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 书籍介绍
    public function actionBook() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Book::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Book');
        $sort->applyOrder($criteria);

        $models = Book::model()->findAll($criteria);

        $this->render('book', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 下载
    public function actionDownload() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Download::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Download');
        $sort->applyOrder($criteria);

        $models = Download::model()->findAll($criteria);

        $this->render('download', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 电子相册
    public function actionAlbum() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Album::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Album');
        $sort->applyOrder($criteria);

        $models = Album::model()->findAll($criteria);

        $this->render('album', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 历史上的今天
    public function actionTodayOnHistory() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(TodayOnHistory::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('TodayOnHistory');
        $sort->applyOrder($criteria);

        $models = TodayOnHistory::model()->findAll($criteria);

        $this->render('todayOnHistory', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 招聘
    public function actionJob() {
        $criteria = new CDbCriteria;
        $criteria->condition = 'status = ' . Options::STATUS_PENDING_DELETE;

        $pages = new CPagination(Job::model()->count($criteria));
        $pages->pageSize = self::PAGE_SIZE;
        $pages->applyLimit($criteria);

        $sort = new CSort('Job');
        $sort->applyOrder($criteria);

        $models = Job::model()->findAll($criteria);

        $this->render('job', array(
            'models' => $models,
            'pages' => $pages,
            'sort' => $sort,
        ));
    }

    // 批量恢复、删除所选记录
    public function actionBatchProcess() {
        $model_type = Yii::app()->request->getPost('model_type'); // 对应的模型
        $model_types = array(
            'Product', 'Post', 'UserCase', 'Personnel', 'Video', 'Music', 'Link',
            'Slide', 'Feedback', 'Book', 'Download', 'Album', 'TodayOnHistory',
            'Job'
        );
        if (Yii::app()->request->isPostRequest && in_array($model_type, $model_types)) {
            $items = Yii::app()->request->getPost('select'); // 已选待操作数据
            $action = Yii::app()->request->getPost('action_list'); // 执行的动作

            foreach ($items as $item) {
                switch ($action) {
                    case 'recycle_bin_undo':
                        CActiveRecord::model($model_type)->updateByPk($item, array('status' => Options::STATUS_PUBLISHED));
                        break;
                    case 'recycle_bin_delete':
                        CActiveRecord::model($model_type)->findByPk($item)->delete();
                        break;
                }
            }

            return $this->redirect(array("recycleBin/{$model_type}"));
        } else {
            throw new CHttpException(400, Yii::t('common', 'invalid_request'));
        }
    }
}
